package org.mpremote.android.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import org.mpremote.android.R;
import org.mpremote.android.activities.abstracts.MPListActivity;
import org.mpremote.android.framework.database.MPDataManager;
import org.mpremote.android.framework.ui.ArtistListAdapter;
import org.mpremote.android.framework.ui.quickaction.QuickAction;
import org.mpremote.android.framework.ui.quickaction.QuickActionItem;
import org.mpremote.android.framework.xml.Album;
import org.mpremote.android.framework.xml.Artist;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Gilad
 * Date: 01/10/2010
 * Time: 17:51:49
 */
public class ArtistsListActivity extends MPListActivity {

    private MPDataManager mpDataManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mpDataManager = new MPDataManager(getApplicationContext());
        List<Artist> artistList = mpDataManager.fetchAllArtists(false);

        setListAdapter(new ArtistListAdapter(this, R.layout.artists_list_activity, artistList));
        ListView listView = getListView();
        listView.setTextFilterEnabled(true);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.artists_list_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        final int itemId = item.getItemId();
        if (itemId == R.id.artists_force_refresh) {
            return super.onOptionsItemSelected(item);
        }
        return super.onOptionsItemSelected(item);
    }

    public void moreButtonClickListener(View view) {
        final QuickActionItem playNow = new QuickActionItem();
        playNow.setTitle("Play Now");
        playNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ArtistsListActivity.this, "Playing", Toast.LENGTH_SHORT).show();
            }
        });
        QuickAction qa = new QuickAction(view);

        qa.addActionItem(playNow);
        qa.setAnimStyle(QuickAction.ANIM_AUTO);
        qa.show();
    }

    public void artistClickListener(View view) {
        RelativeLayout relativeLayout = (RelativeLayout) view.getParent();
        // Why 2? because the child is defined by the xml layout
        final Button artistButton = (Button) relativeLayout.getChildAt(2);
        final List<Album> albums = mpDataManager.fetchAlbumsForArtist((String) artistButton.getText());
        Intent intent = new Intent(ArtistsListActivity.this, AlbumsListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putStringArrayList();
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }

}
